package com.huluobo.a2112mvvmdouyindemo.adapter;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.huluobo.a2112mvvmdouyindemo.R;
import com.huluobo.a2112mvvmdouyindemo.utils.ImageLoader;
import com.huluobo.mvvm_base.bean.VideoBeanResult;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

/**
 * Created by LC on 2024/3/30.
 */
public class MultiVideoAdapter extends BaseMultiItemQuickAdapter<VideoBeanResult.VideoBean, BaseViewHolder> {
    public MultiVideoAdapter() {
        addItemType(0, R.layout.item_videos);
        addItemType(1, R.layout.item_video_live);
        addItemType(2, R.layout.item_video_full);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, VideoBeanResult.VideoBean videoBean) {
        StandardGSYVideoPlayer player = baseViewHolder.getView(R.id.item_video_gsyPlayer);
        player.getBackButton().setVisibility(View.GONE);
        player.setUp(videoBean.getVideopath(), true, "");

        //根据不同视频类型type,来设置赋值
        switch (videoBean.getType()) {
            case 0:
            case 2:
                //设置全屏视频
                setVideoOption(baseViewHolder, videoBean);
                break;
            case 1:
                //设置直播视频赋值
                setLiveOption(baseViewHolder, videoBean);
                break;
        }


    }

    private void setLiveOption(BaseViewHolder baseViewHolder, VideoBeanResult.VideoBean videoBean) {
        setCommonVideoInfo(baseViewHolder, videoBean);
        //TODO 需要跳转直播间
    }

    private void setVideoOption(BaseViewHolder baseViewHolder, VideoBeanResult.VideoBean videoBean) {
        setCommonVideoInfo(baseViewHolder, videoBean);
        //右下方区域赋值
        //头像
        ImageView headerView = baseViewHolder.getView(R.id.item_video_header_iv);
        ImageLoader.getInstance().loadCircleImage(getContext(), videoBean.getHeadpath(), headerView);
        //关注状态 ,0代表未关注,展示+号,1代表已关注,隐藏+号
        if (videoBean.getGuanzhu() == 0) {
            baseViewHolder.getView(R.id.item_video_add_iv).setVisibility(View.VISIBLE);
        } else {
            baseViewHolder.getView(R.id.item_video_add_iv).setVisibility(View.GONE);
        }
        //点赞状态,0代表未点赞(白心),1代表点赞(红心)
        if (videoBean.getDianzan() == 0) {
            baseViewHolder.setImageResource(R.id.item_video_like_iv, R.drawable.ic_xihuan);
        } else {
            baseViewHolder.setImageResource(R.id.item_video_like_iv, R.drawable.ic_xihuanred);
        }
        //点赞数量
        int likeCount = videoBean.getLike_count();
        baseViewHolder.setText(R.id.item_video_like_tv, likeCount < 10000 ? String.valueOf(likeCount) :
                likeCount / 10000 + "W");
        //评论数量
        baseViewHolder.setText(R.id.item_video_comment_tv, "5.5W");
        //收藏数量
        baseViewHolder.setText(R.id.item_video_follow_tv, "3.2W");
        //分享数量
        baseViewHolder.setText(R.id.item_video_share_count_tv, "2.2W");

        //碟片动画
        ImageView musicPlayer = baseViewHolder.getView(R.id.item_video_music_player_iv);
        ImageLoader.getInstance().loadCircleImage(getContext(), videoBean.getHeadpath(), musicPlayer);

        ObjectAnimator animator = ObjectAnimator.ofFloat(musicPlayer, "Rotation", 0, 360);
        animator.setDuration(3000);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setInterpolator(new LinearInterpolator());
        animator.start();
    }

    //设置通用视频信息(左下角坐着,地址,描述)
    private void setCommonVideoInfo(BaseViewHolder baseViewHolder, VideoBeanResult.VideoBean videoBean) {
        baseViewHolder.setText(R.id.item_video_address_tv, videoBean.getAddress());
        baseViewHolder.setText(R.id.item_video_authname_tv, videoBean.getAuthname());
        baseViewHolder.setText(R.id.item_video_caption_tv, videoBean.getCaption());
        baseViewHolder.setText(R.id.item_video_pao_tv, videoBean.getAuthname() + "的原创作品");
    }
}
